package com.hundredapp.oa.repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.GenericDao;
import com.hundredapp.oa.domain.OaContract;
import com.hundredapp.oa.util.SequenceUtil;

@Repository
public class ContractMangerImpl implements ContractManager{

	@Autowired
	private GenericDao<OaContract, Integer> contractDao;

	@Override
	public List<OaContract> find(String condition) {
		return contractDao.find(condition);
	}

	@Override
	public Integer add(OaContract contract) {
		SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd");
		Date now = new Date();
		String date = formatter.format(now);
		int size = contractDao.find(" 1=1 and createTime between '"+date+" 00:00:01' and '"+date+" 23:59:59' ").size();
		contract.setNumber(date + "x" + (size+1));
		return contractDao.save(contract);
	}

	@Override
	public void delete(Integer contractid) {
		contractDao.remove(contractDao.findById(contractid));
		
	}

	@Override
	public List<OaContract> find(String condition, int start, int limit) {
		return contractDao.find(condition, start, limit);
	}

	@Override
	public int count(String condition) {
		return contractDao.count(condition);
	}

	public void setContractDao(GenericDao<OaContract, Integer> contractDao) {
		this.contractDao = contractDao;
	}

	@Override
	public void save(OaContract contract) {
		contractDao.saveOrUpdate(contract);
	}

	@Override
	public OaContract findById(Integer id) {
		return contractDao.findById(id);
	}

	@Override
	public void update(OaContract payment) {
		contractDao.modify(payment);
	}

	@Override
	public List<OaContract> findNonUse(String condition) {
		return contractDao.find(condition + " and oaStudents.size = 0");
	}

	
	
}
